// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   HistoryScrPanel.java

package com.boci.clientapp.history;

import com.boci.clientapp.history.action.HistoryAction;
import com.boci.clientapp.history.constant.HistoryConst;
import com.boci.clientapp.history.vo.TransactionHistoryDetailVO;
import com.boci.clientapp.history.vo.TransactionHistoryVO;
import com.boci.clientapp.main.ClientAppLoader;
import com.boci.common.constant.ClientAppConst;
import com.boci.common.ui.AbstractPanel;
import com.boci.common.ui.control.*;
import com.boci.common.util.*;
import com.boci.common.vo.ResponseVO;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.*;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.table.*;

public class HistoryScrPanel extends AbstractPanel
{

    public HistoryScrPanel(Locale pLocale, String pModuleName, String pScreenName)
    {
        super(pLocale, pModuleName, pScreenName);
        msgConf = ClientAppLoader.getMessagesConfigurator();
        initGUI(pLocale);
        setText(pLocale);
    }

    public void setText(Locale pLocale)
    {
        super.setText(pLocale);
        currentLocale = pLocale;
        setFont(pLocale);
        setPageNo(pLocale);
        nameRenderer.setText(pLocale);
        btnPrevious.setText(msgConf.getUIMessages("HISTORY.PREVIOUS", pLocale));
        btnNext.setText(msgConf.getUIMessages("HISTORY.NEXT", pLocale));
        lblTransaction.setText(msgConf.getUIMessages("HISTORY.TRANSACTION", pLocale));
        lblTimePeriod.setText(msgConf.getUIMessages("HISTORY.TIME_PERIOD", pLocale));
        btnGo.setText(msgConf.getUIMessages("HISTORY.GO", pLocale));
        int intTranIdx = cmbTransaction.getSelectedIndex();
        cmbTransaction.setModel(new DefaultComboBoxModel(msgConf.getMsgArray("HISTORY.", ClientAppLoader.getHistory_Transactions(), pLocale)));
        cmbTransaction.setSelectedIndex(intTranIdx);
        int intTimeIdx = cmbTimePeriod.getSelectedIndex();
        cmbTimePeriod.setModel(new DefaultComboBoxModel(msgConf.getMsgArray("HISTORY.", ClientAppLoader.getHistory_TimePeriods(), pLocale)));
        cmbTimePeriod.setSelectedIndex(intTimeIdx);
        String strHistoryColName[] = {
            msgConf.getUIMessages("HISTORY.COL_DATE", pLocale), msgConf.getUIMessages("HISTORY.COL_TRANSACTION", pLocale), msgConf.getUIMessages("HISTORY.COL_NAME", pLocale), msgConf.getUIMessages("HISTORY.COL_CODE", pLocale), msgConf.getUIMessages("HISTORY.COL_PRICE", pLocale), msgConf.getUIMessages("HISTORY.COL_QUANTITY", pLocale), msgConf.getUIMessages("HISTORY.COL_NET_AMOUNT", pLocale), msgConf.getUIMessages("HISTORY.COL_REMARK", pLocale)
        };
        tmHistory.setColumnName(strHistoryColName);
        tmHistory.fireTableStructureChanged();
        for(int i = 0; i < tblHistory.getColumnCount(); i++)
        {
            TableColumn column = tblHistory.getColumnModel().getColumn(i);
            if(i == 2 || i == 1 || i == 7)
                column.setCellRenderer(nameRenderer);
            else
            if(i == 3)
                column.setCellRenderer(codeRenderer);
            else
            if(i == 4 || i == 5 || i == 6)
                column.setCellRenderer(numericRenderer);
            else
                column.setCellRenderer(stringRenderer);
            column.setPreferredWidth(HistoryConst.COL_WIDTH_HISTORY[i]);
        }

    }

    private void setFont(Locale pLocale)
    {
        btnPrevious.setFont(ClientAppLoader.getSmallBoldTextFont(pLocale));
        btnNext.setFont(ClientAppLoader.getSmallBoldTextFont(pLocale));
        lblTransaction.setFont(ClientAppLoader.getSmallBoldTextFont(pLocale));
        lblTimePeriod.setFont(ClientAppLoader.getSmallBoldTextFont(pLocale));
        btnGo.setFont(ClientAppLoader.getPlainButtonFont(pLocale));
        cmbTransaction.setFont(ClientAppLoader.getSmallPlainTextFont(pLocale));
        cmbTimePeriod.setFont(ClientAppLoader.getSmallPlainTextFont(pLocale));
        stringRenderer.setFont(ClientAppLoader.getSmallPlainTextFont(pLocale));
        numericRenderer.setFont(ClientAppLoader.getSmallPlainTextFont(pLocale));
        codeRenderer.setFont(ClientAppLoader.getSmallPlainTextFont(pLocale));
        tblHistory.getTableHeader().setFont(ClientAppLoader.getSmallPlainTextFont(pLocale));
    }

    private void initHistorySclPane(Locale pLocale)
    {
        tblHistory = new JTable();
        tmHistory = new CMTableModel();
        historySclPane = new JScrollPane();
        nameRenderer = new NameRenderer(pLocale);
        stringRenderer = new StringRenderer();
        numericRenderer = new StringRenderer();
        codeRenderer = new StringRenderer();
        numericRenderer.setHorizontalAlignment(4);
        codeRenderer.setHorizontalAlignment(0);
        tblHistory.setModel(tmHistory);
        tblHistory.getTableHeader().setReorderingAllowed(false);
        tblHistory.setSelectionMode(1);
        tblHistory.setCellSelectionEnabled(false);
        historySclPane.setViewportView(tblHistory);
        historySclPane.getViewport().setOpaque(false);
        historySclPane.setOpaque(false);
        historySclPane.setBorder(new EtchedBorder(1));
    }

    private void initHeaderPanel()
    {
        headerPanel = new JPanel();
        headerPanel.setPreferredSize(new Dimension(0, 22));
        headerPanel.setOpaque(false);
        headerPanel.setLayout(null);
        btnPrevious = new JButton();
        btnPrevious.setBounds(1, 1, 50, 20);
        btnPrevious.setActionCommand("PREVIOUS");
        btnPrevious.addActionListener(this);
        headerPanel.add(btnPrevious);
        lblPage = new JLabel();
        lblPage.setHorizontalAlignment(0);
        lblPage.setBounds(52, 1, 80, 20);
        headerPanel.add(lblPage);
        btnNext = new JButton();
        btnNext.setBounds(133, 1, 50, 20);
        btnNext.setActionCommand("NEXT");
        btnNext.addActionListener(this);
        headerPanel.add(btnNext);
        lblTransaction = new JLabel();
        lblTransaction.setHorizontalAlignment(4);
        lblTransaction.setBounds(187, 1, 90, 20);
        headerPanel.add(lblTransaction);
        cmbTransaction = new JComboBox();
        cmbTransaction.setBounds(280, 1, 190, 20);
        headerPanel.add(cmbTransaction);
        lblTimePeriod = new JLabel();
        lblTimePeriod.setHorizontalAlignment(4);
        lblTimePeriod.setBounds(472, 1, 90, 20);
        headerPanel.add(lblTimePeriod);
        cmbTimePeriod = new JComboBox();
        cmbTimePeriod.setBounds(565, 1, 140, 20);
        headerPanel.add(cmbTimePeriod);
        btnGo = new JButton();
        btnGo.setBounds(710, 1, 22, 20);
        btnGo.setMargin(new Insets(0, 0, 0, 0));
        btnGo.setActionCommand("GO");
        btnGo.addActionListener(this);
        headerPanel.add(btnGo);
    }

    private void initGUI(Locale pLocale)
    {
        initHeaderPanel();
        initHistorySclPane(pLocale);
        setLayout(new BorderLayout());
        setPreferredSize(null);
        add(headerPanel, "North");
        add(historySclPane, "Center");
        JPanel gapPanel = new JPanel();
        gapPanel.setOpaque(false);
        gapPanel.setPreferredSize(new Dimension(0, 15));
        add(gapPanel, "South");
    }

    public void actionPerformed(ActionEvent e)
    {
        PerformanceLogger.startAppTimer();
        String strActionType = e.getActionCommand();
        HistoryAction historyAction = new HistoryAction();
        historyAction.doProcess(getInputValue(strActionType), strActionType);
        PerformanceLogger.stopAppTimer();
        PerformanceLogger.doLog((new StringBuilder()).append(getScreenName()).append(".").append(strActionType).toString());
    }

    public void refresh()
    {
        intPage = 1;
        cmbTransaction.setSelectedIndex(0);
        cmbTimePeriod.setSelectedIndex(0);
        String strActionType = "GO";
        HistoryAction historyAction = new HistoryAction();
        historyAction.doProcess(getInputValue(strActionType), strActionType);
    }

    public void refresh(ResponseVO pResponseVO)
    {
        if(pResponseVO == null)
        {
            Object objTableData[][] = new Object[0][8];
            tmHistory.setData(objTableData);
            tmHistory.fireTableDataChanged();
            historySclPane.setPreferredSize(new Dimension(0, 0));
            historySclPane.setMinimumSize(new Dimension(0, 0));
            historySclPane.getViewport().setViewPosition(new Point(0, 0));
            lblPage.setText("");
            btnPrevious.setEnabled(false);
            btnNext.setEnabled(false);
        } else
        if(pResponseVO instanceof TransactionHistoryVO)
        {
            TransactionHistoryVO historyVO = (TransactionHistoryVO)pResponseVO;
            currentHistoryVO = historyVO;
            ArrayList historyDetailVOList = historyVO.getTransactionHistoryDetailVOList();
            Object objTableData[][] = new Object[historyDetailVOList.size()][8];
            for(int i = 0; i < historyDetailVOList.size(); i++)
            {
                TransactionHistoryDetailVO histDetailVO = (TransactionHistoryDetailVO)historyDetailVOList.get(i);
                String strNames[] = {
                    histDetailVO.getName(), histDetailVO.getName_chi(), histDetailVO.getName_chs()
                };
                String strTransactions[] = {
                    msgConf.getUIMessages(histDetailVO.getDescription(), ClientAppConst.EN_US), msgConf.getUIMessages(histDetailVO.getDescription(), ClientAppConst.ZH_HK), msgConf.getUIMessages(histDetailVO.getDescription(), ClientAppConst.ZH_CN)
                };
                String strRemark[] = {
                    histDetailVO.getRemark(), histDetailVO.getRemark_chi(), histDetailVO.getRemark_chs()
                };
                objTableData[i][0] = histDetailVO.getDate();
                objTableData[i][1] = strTransactions;
                objTableData[i][2] = strNames;
                objTableData[i][3] = histDetailVO.getSymbol();
                String strPrice = StringUtil.formatPriceValue(histDetailVO.getPrice());
                String strAmount = StringUtil.formatPriceValue(histDetailVO.getAmount());
                String strQty = StringUtil.formatQtyValue(histDetailVO.getQuantity());
                objTableData[i][4] = strPrice;
                objTableData[i][5] = strQty;
                objTableData[i][6] = strAmount;
                objTableData[i][7] = strRemark;
            }

            tmHistory.setData(objTableData);
            tmHistory.fireTableDataChanged();
            historySclPane.setPreferredSize(new Dimension(0, 0));
            historySclPane.setMinimumSize(new Dimension(0, 0));
            historySclPane.getViewport().setViewPosition(new Point(0, 0));
            setPageNo(currentLocale);
        }
    }

    private void setPageNo(Locale pLocale)
    {
        if(currentHistoryVO == null)
            return;
        String strPageLabel = msgConf.getUIMessages("HISTORY.PAGE", pLocale);
        strPageLabel = strPageLabel.replaceAll(msgConf.getPageRegex(), currentHistoryVO.getPage());
        strPageLabel = strPageLabel.replaceAll(msgConf.getTotalPageRegex(), currentHistoryVO.getTotalPage());
        lblPage.setText(strPageLabel);
        lblPage.setFont(ClientAppLoader.getSmallBoldTextFont(pLocale));
        btnPrevious.setEnabled(true);
        btnNext.setEnabled(true);
        if(currentHistoryVO.getPage().equals("1"))
            btnPrevious.setEnabled(false);
        if(currentHistoryVO.getTotalPage().equals(currentHistoryVO.getPage()))
        {
            btnNext.setEnabled(false);
            setDefFocus();
        }
        try
        {
            intPage = Integer.parseInt(currentHistoryVO.getPage());
        }
        catch(Exception e)
        {
            intPage = 0;
        }
    }

    public HashMap getInputValue(String pActionType)
    {
        HashMap inpMap = new HashMap();
        if(pActionType.equals("NEXT"))
        {
            intPage++;
            cmbTimePeriod.setSelectedIndex(intCurTimePeriodIdx);
            cmbTransaction.setSelectedIndex(intCurTransactionIdx);
        } else
        if(pActionType.equals("PREVIOUS"))
        {
            intPage--;
            cmbTimePeriod.setSelectedIndex(intCurTimePeriodIdx);
            cmbTransaction.setSelectedIndex(intCurTransactionIdx);
        } else
        if(pActionType.equals("GO"))
        {
            intPage = 1;
            intCurTimePeriodIdx = cmbTimePeriod.getSelectedIndex();
            intCurTransactionIdx = cmbTransaction.getSelectedIndex();
        }
        String strTimePeriods[] = ClientAppLoader.getHistory_TimePeriods();
        String strTransactions[] = ClientAppLoader.getHistory_Transactions();
        inpMap.put("select", strTimePeriods[intCurTimePeriodIdx]);
        inpMap.put("FilterStr", strTransactions[intCurTransactionIdx]);
        inpMap.put("page", String.valueOf(intPage));
        return inpMap;
    }

    public boolean validate(String pActionType)
    {
        return true;
    }

    public void setDefFocus()
    {
        SwingUtilities.invokeLater(new Runnable() {

            public void run()
            {
                if(btnPrevious.isEnabled())
                    btnPrevious.requestFocus();
                else
                if(btnNext.isEnabled())
                    btnNext.requestFocus();
                else
                    cmbTransaction.requestFocus();
            }
        }
);
    }

    private JPanel headerPanel;
    private JButton btnPrevious;
    private JLabel lblPage;
    private JButton btnNext;
    private JLabel lblTransaction;
    private JComboBox cmbTransaction;
    private JLabel lblTimePeriod;
    private JComboBox cmbTimePeriod;
    private JButton btnGo;
    private JTable tblHistory;
    private CMTableModel tmHistory;
    private JScrollPane historySclPane;
    private NameRenderer nameRenderer;
    private StringRenderer stringRenderer;
    private StringRenderer numericRenderer;
    private StringRenderer codeRenderer;
    private MessagesConfigurator msgConf;
    private int intPage;
    private int intCurTimePeriodIdx;
    private int intCurTransactionIdx;
    private Locale currentLocale;
    private TransactionHistoryVO currentHistoryVO;



}
